package com.sunshine.lnuplus.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.recyclerview.widget.RecyclerView
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.callback.LoadLessonFileCallBack
import com.sunshine.lnuplus.model.ClassBean
import com.sunshine.lnuplus.utils.CharacterParser
import com.sunshine.lnuplus.utils.readExcel
import java.util.*
import kotlin.Comparator
import kotlin.collections.ArrayList

/**
 * Created by sunshine on 2020/3/21
 */

class LoadFileAdapter(
    private val activity: AppCompatActivity,
    private val loadLessonFileCallBack: LoadLessonFileCallBack) : RecyclerView.Adapter<LoadFileAdapter.RecyclerHolder>() {

    private var lessonList: ArrayList<Map<String, String>>? = null
    private lateinit var classNameList: ArrayList<ClassBean>

    init {
        lessonList = readExcel(activity)
        if(lessonList != null){
            classNameList = ArrayList()
            lessonList!!.forEach {
                it["banji"]?.split(",")?.forEach {
                    val classBean = ClassBean("", it, "")
                    if(!classNameList.contains(classBean)){
                        classNameList.add(classBean)
                    }
                }
            }
        }
        classNameList.forEach {
            val grade = it.lessonName.substring(0, 2)
            it.newLessonName = it.lessonName.substring(2, it.lessonName.length) + " " + grade + "级"
            val pinyin = CharacterParser.instance.getSelling(it.newLessonName.substring(0, it.newLessonName.length - 1))
            val letter = pinyin?.substring(0, 1)
            if (letter!!.matches(Regex("\\w"))) {// 匹配拼音第一个位置是否为a-zA-Z
                it.pinyin = letter.toUpperCase()
            } else {
                it.pinyin = "#"
            }
        }
        Collections.sort(classNameList, PinyinComparable())
    }

    class RecyclerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val button: Button = itemView.findViewById(R.id.button_choose_classname)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerHolder {
        return RecyclerHolder(LayoutInflater.from(activity).inflate(R.layout.item_classname, parent, false))
    }

    override fun getItemCount(): Int {
        return classNameList.size
    }

    override fun onBindViewHolder(holder: RecyclerHolder, position: Int) {
        if(activity.getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE).getBoolean("night", false)){
            holder.button.setTextColor(Color.WHITE)
        }else{
            holder.button.setTextColor(Color.BLACK)
        }
        holder.button.text = classNameList[position].newLessonName

        holder.button.setOnClickListener {
            searchMyLesson(classNameList[position].lessonName)
        }
    }

    private fun searchMyLesson(myClass: String) {
        val myLessonList = ArrayList<Map<String, String>>()
        lessonList?.forEach {
            if(it["banji"]!!.split(",").contains(myClass)){
                myLessonList.add(it)
            }
        }
        loadLessonFileCallBack.onSuccess(myLessonList)
    }

    fun getIndex(str: String?): Int {
        classNameList.forEach {
            if(it.pinyin == str){
                return classNameList.indexOf(it)
            }
        }
        return -1
    }
}

class PinyinComparable : Comparator<ClassBean>{
    override fun compare(o1: ClassBean?, o2: ClassBean?): Int {
        return o1!!.pinyin.compareTo(o2!!.pinyin)
    }
}

